package cn.jx.asc.controller;

import cn.jx.asc.common.constant.Contants;
import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;
import cn.jx.asc.parseupload.OkHttpUtil2;
import cn.jx.asc.pojo.bo.MountainBO;
import cn.jx.asc.pojo.bo.PipeBO;
import cn.jx.asc.pojo.bo.PipeInfoBO;
import cn.jx.asc.pojo.bo.QueryBO;
import cn.jx.asc.pojo.entity.Mountain;
import cn.jx.asc.pojo.entity.Notice;
import cn.jx.asc.pojo.vo.MountainVO;
import cn.jx.asc.pojo.vo.PipeVO;
import cn.jx.asc.service.CustomDataService;
import cn.jx.asc.service.MountainService;
import cn.jx.asc.service.PipeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lhg
 */

@Api(value = "数据中心", tags = {"数据中心的相关接口"})
@RestController
@RequestMapping("/dataCenter")
public class DataCenterController {

    @Autowired
    private MountainService mountainService;

    @Autowired
    private PipeService pipeService;

    @Autowired
    private CustomDataService customDataService;

    @ApiOperation(value = "查询山体所有记录", notes = "查询山体所有记录", httpMethod = "GET")
    @GetMapping("/queryAllMountainData")
    public Result queryAllMountainData(
            @ApiParam(name = "current",value = "第几页")
            @RequestParam(required = false) Long current,
            @ApiParam(name = "size",value = "条数")
            @RequestParam(required = false) Long size){

        if (current == null){
            current = Contants.CURRENT;
        }

        if (size == null){
            size = Contants.SIZE;
        }
        final MountainBO mountainBO = mountainService.queryAllMountainData(current, size);

        return ResultUtils.resultWithData(ResultStatus.SUCCESS,mountainBO);
    }

    @ApiOperation(value = "查询山体时间段数据", notes = "查询山体时间段数据", httpMethod = "POST")
    @PostMapping("/queryMountainDataByTime")
    public Result queryMountainDataByTime(@RequestBody QueryBO queryBO) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (queryBO.getCurrent() == null){
            queryBO.setCurrent( Contants.CURRENT);
        }

        if (queryBO.getSize() == null){
            queryBO.setSize(Contants.SIZE);
        }
        final MountainBO mountainBO = mountainService.queryMountainDataByTime(queryBO.getCurrent(),
               queryBO.getSize(),dateFormat.parse(queryBO.getStartTime()),dateFormat.parse(queryBO.getEndTime()));

        return ResultUtils.resultWithData(ResultStatus.SUCCESS,mountainBO);
    }

    @ApiOperation(value = "查询注液管所有记录", notes = "查询注液管所有记录", httpMethod = "GET")
    @GetMapping("/queryAllPipeData")
    public Result queryAllPipeData(
            @ApiParam(name = "current",value = "第几页")
            @RequestParam(required = false)  Long current,
            @ApiParam(name = "size",value = "条数" )
            @RequestParam(required = false) Long size){

        if (current == null){
            current = Contants.CURRENT;
        }

        if (size == null){
            size = Contants.SIZE;
        }
        final PipeBO pipeBO = pipeService.queryAllPipeData(current, size);


        return ResultUtils.resultWithData(ResultStatus.SUCCESS,pipeBO);
    }


    @ApiOperation(value = "查询注液管时间段数据", notes = "查询注液管时间段数据", httpMethod = "POST")
    @PostMapping("/queryPipeDataByTime")
    public Result queryPipeDataByTime(@RequestBody QueryBO queryBO) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (queryBO.getCurrent() == null){
            queryBO.setCurrent( Contants.CURRENT);
        }

        if (queryBO.getSize() == null){
            queryBO.setSize(Contants.SIZE);
        }
        final PipeBO pipeBO = pipeService.queryPipeDataByTime(queryBO.getCurrent(),
                queryBO.getSize(), dateFormat.parse(queryBO.getStartTime()), dateFormat.parse(queryBO.getEndTime()));

        return ResultUtils.resultWithData(ResultStatus.SUCCESS,pipeBO);
    }
    @ApiOperation(value = "查询注液管最新数据", notes = "查询注液管时间段数据", httpMethod = "GET")
    @GetMapping("/queryPipeDataByTd")
    public Result queryPipeDataByTd(Integer pipeId) {
         PipeInfoBO pipeInfoBO = pipeService.queryPipeInfoById(pipeId);

        return ResultUtils.resultWithData(ResultStatus.SUCCESS,pipeInfoBO);
    }


    @ApiOperation(value = "查询矿山最新数据", notes = "查询矿山最新数据", httpMethod = "GET")
    @GetMapping("/queryPipeMountainByTd")
    public Result queryPipeMountainByTd(Integer mountainId) {
        final MountainVO mountainVO = mountainService.queryMountainInfoById(mountainId);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,mountainVO);
    }
    @ApiOperation(value = "查询滑坡概率", notes = "查询滑坡概率", httpMethod = "GET")
    @GetMapping("/queryProLandslide")
    public Result queryProLandslide() {
        final Double aDouble = customDataService.calculateLandslide();
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,aDouble);
    }

    @ApiOperation(value = "查询近30分钟注液管数据", notes = "查询近30分钟注液管数据", httpMethod = "GET")
    @GetMapping("/queryPipeForCharts")
    public Result queryPipeForCharts() {

        final Date d1 = new Date();
        final Date d2 = new Date(d1.getTime()-1000*60*30);
        final List<PipeVO> list = pipeService.queryPipeDataByTime(d2, d1);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,list);
    }

    @ApiOperation(value = "查询近30分钟矿山数据", notes = "查询近30分钟矿山数据", httpMethod = "GET")
    @GetMapping("/queryMountainForCharts")
    public Result queryMountainForCharts() {
        final Date d1 = new Date();
        final Date d2 = new Date(d1.getTime()-1000*60*30);
        final List<MountainVO> list = mountainService.queryMountainDataByTime(d2, d1);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,list);
    }

    @PostMapping("/cmd")
    public String cmd(){
        OkHttpUtil2 ok=new OkHttpUtil2();
        String ffff = ok.issue("A,3,0");
        return  ffff;

    }

}
